//=============================================================================
/**
 * @file	gym_init.c
 * @brief	ジム仕掛け初期化とか
 * @author	Nozomu Saito
 *
 * モジュール名：GYM_
 *
 */
//=============================================================================
#include "common.h"
#include "fieldsys.h"
#include "fieldmap_work.h"
#include "system/snd_tool.h"
#include "include/savedata/gimmickwork.h"
#include "../fielddata/build_model/build_model_id.h"

#include "field_gimmick_def.h"
#include "sysflag.h"

#include "gym_def.h"

#include "gym_local.h"
#include "gym_init.h"

#define DUST_BOX_NUM	(15)

typedef struct SWITCH_TBL_tag
{
	int Num;
	u8 Idx[4];
}SWITCH_TBL;

//電気ジムスイッチあたり表
static const SWITCH_TBL SwitchTbl[DUST_BOX_NUM] = {
	{ 2,{1, 5, 255,255}  },
	{ 3,{0, 2, 6, 255}   },
	{ 3,{1, 3, 7, 255}   },
	{ 3,{2, 4, 8, 255}   },
	{ 2,{3, 9, 255, 255} },
	{ 3,{0, 6, 10, 255}  },
	{ 4,{1, 5, 7, 11}    },
	{ 4,{2, 6, 8, 12}    },
	{ 4,{3, 7, 9, 13}    },
	{ 3,{4, 8, 14, 255}  },
	{ 2,{5, 11, 255, 255} },
	{ 3,{6, 10,12, 255}   },
	{ 3,{7, 11,13, 255}   },
	{ 3,{8, 12,14, 255}   },
	{ 2,{9, 13, 255, 255} },
};

//---------------------------------------------------------------------------
/**
 * @brief	ゴーストジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitGhostGym(FIELDSYS_WORK *fsys)
{
	int i;
	GIMMICKWORK *work;
	GHOST_GYM_WORK *ghost_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_GHOST_GYM);

	ghost_gym_work = (GHOST_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_GHOST_GYM);

	//トレーナのライト状態を初期化（全員ライト持ち）
	for(i=0;i<4;i++){
		ghost_gym_work->LightOff[i] = 0;
	}
}

//---------------------------------------------------------------------------
/**
 * @brief	格闘ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitCombatGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	COMBAT_GYM_WORK *combat_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_COMBAT_GYM);

	combat_gym_work = (COMBAT_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_COMBAT_GYM);
}

//---------------------------------------------------------------------------
/**
 * @brief	電気ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitElecGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	ELEC_GYM_WORK *elec_gym_work;

	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_ELEC_GYM_K);

	elec_gym_work = (ELEC_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_ELEC_GYM_K);

	if ( MyStatus_GetBadgeFlag(SaveData_GetMyStatus(fsys->savedata), BADGE_ID_T06) ){		//ジムバッジ入手済み？
		//スイッチの抽選はなし
		elec_gym_work->SwitchPos1 = 0;
		elec_gym_work->SwitchPos2 = 0;
		//スイッチ状態を開錠状態に
		elec_gym_work->Switch1OK = 1;
		elec_gym_work->Switch2OK = 1;
	}else{
		//スイッチの抽選
		GYM_EntryElecGymSwitch(fsys);
		//スイッチ状態をクリア
		elec_gym_work->Switch1OK = 0;
		elec_gym_work->Switch2OK = 0;
	}
}

//--------------------------------------------------------------
/**
 * @brief   スイッチの抽選
 *
 * @param  fsys	フィールドシステムポインタ
 *
 * @retval  none
 *
 */
//--------------------------------------------------------------
void GYM_EntryElecGymSwitch(FIELDSYS_WORK *fsys)
{
	const SWITCH_TBL *tbl;
	u8 idx;
	GIMMICKWORK *work;
	ELEC_GYM_WORK *elec_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	elec_gym_work = (ELEC_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_ELEC_GYM_K);

	elec_gym_work->SwitchPos1 = gf_rand()%DUST_BOX_NUM;
	tbl = &SwitchTbl[elec_gym_work->SwitchPos1];
	idx = gf_rand()%tbl->Num;
	elec_gym_work->SwitchPos2 = tbl->Idx[idx];

	OS_Printf("あたりスイッチは %d, %d\n",elec_gym_work->SwitchPos1,elec_gym_work->SwitchPos2);
}

//---------------------------------------------------------------------------
/**
 * @brief	飛行ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitSkyGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	SKY_GYM_WORK *sky_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_SKY_GYM);

	sky_gym_work = (SKY_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_SKY_GYM);

	sky_gym_work->Lift = GYM_SKY_LIFT_LV1;
}

//---------------------------------------------------------------------------
/**
 * @brief	虫ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitInsectGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	INSECT_GYM_WORK *insect_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_INSECT_GYM);

	insect_gym_work = (INSECT_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_INSECT_GYM);

	insect_gym_work->Lift[0] = GYM_INSECT_LIFT_DEF1;
	insect_gym_work->Lift[1] = GYM_INSECT_LIFT_DEF2;
	insect_gym_work->Lift[2] = GYM_INSECT_LIFT_DEF3;
	insect_gym_work->Lift[3] = GYM_INSECT_LIFT_DEF4;

	insect_gym_work->RailState = GYM_INSECT_RAIL_ALL;

}

//---------------------------------------------------------------------------
/**
 * @brief	ドラゴンジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitDragonGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	DRAGON_GYM_WORK *dragon_gym_work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_DRAGON_GYM);

	dragon_gym_work = (DRAGON_GYM_WORK*)GIMMICKWORK_Get(work, FLD_GIMMICK_DRAGON_GYM);

	dragon_gym_work->FloorX[0] = FLOOR_POS_X_FIRST;
	dragon_gym_work->FloorZ[0] = FLOOR_POS_Z_FIRST;
	dragon_gym_work->RevState[0] = FLOOR_REV_0;
	dragon_gym_work->FloorX[1] = FLOOR_POS_X_SECOND;
	dragon_gym_work->FloorZ[1] = FLOOR_POS_Z_SECOND;
	dragon_gym_work->RevState[1] = FLOOR_REV_90;
	dragon_gym_work->FloorX[2] = FLOOR_POS_X_THIRD;
	dragon_gym_work->FloorZ[2] = FLOOR_POS_Z_THIRD;
	dragon_gym_work->RevState[2] = FLOOR_REV_0;

}

//---------------------------------------------------------------------------
/**
 * @brief	毒ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitPoisonGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_POISON_GYM);
}

//---------------------------------------------------------------------------
/**
 * @brief	万能ジム:初期化
 *
 * @param	fsys	フィールドシステムポインタ
 *
 * @return	none
 */
//---------------------------------------------------------------------------
void GYM_InitAlmGym(FIELDSYS_WORK *fsys)
{
	GIMMICKWORK *work;
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	GIMMICKWORK_Assign(work, FLD_GIMMICK_ALMIGHTY_GYM);
}

//--------------------------------------------------------------
/**
 * @brief   ギミックコードが指定IDのコードと同一かチェック
 *
 * @param	fsys	フィールドシステムポインタ
 * @param   gimmick_id		ギミックコード
 *
 * @retval  TRUE:一致。　FALSE:不一致
 *
 * gym.cを専用のオーバーレイ領域(fieldmap_sub_gym)に移した為、
 * gym.cの関数を呼ぶ前にこれでチェックする
 */
//--------------------------------------------------------------
BOOL GYM_GimmickCodeCheck(FIELDSYS_WORK *fsys, int gimmick_id)
{
	GIMMICKWORK *work;

	//ギミックワーク取得
	work = SaveData_GetGimmickWork(GameSystem_GetSaveData(fsys));
	//ギミックコードを取得
	if(gimmick_id == GIMMICKWORK_GetAssignID(work)){
		return TRUE;
	}
	return FALSE;
}